#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using BreakOutBattles.Menus;
using BreakOutBattles.Game;
using BreakOutBattles.IO;
using BreakOutBattles.Sound;
#endregion

namespace BreakOutBattles
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class GameMain : Microsoft.Xna.Framework.Game
    {
        #region Fields
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        IOManager ioManager;

        MainMenu mainMenu;
        CreateGameMenu createGameMenu;
        JoinGameMenu joinGameMenu;
        JoinWaitScreen joinWaitScreen;
        PauseMenu pauseMenu;
        GameManager gameManager;
        MatchReportViewer matchReportViewer;
        #endregion

        public GameMain()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = 1280;
            graphics.PreferredBackBufferHeight = 720;
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            ioManager = IOManager.Instance;
            ioManager.Initialize(Content);

            SoundManager.Initialize(Content.RootDirectory);

            spriteBatch = new SpriteBatch(GraphicsDevice);
            mainMenu = new MainMenu(this, spriteBatch);
            createGameMenu = new CreateGameMenu(this, spriteBatch);
            joinGameMenu = new JoinGameMenu(this, spriteBatch);
            joinWaitScreen = new JoinWaitScreen();
            pauseMenu = new PauseMenu(this, spriteBatch);
            matchReportViewer = new MatchReportViewer();
            gameManager = new GameManager();

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            mainMenu.LoadContent();
            createGameMenu.LoadContent();
            joinGameMenu.LoadContent();
            joinWaitScreen.LoadContent();
            pauseMenu.LoadContent();
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        private void HandleInput(GameTime gameTime)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                GameState.CurrentState = GameStates.GAME_PAUSED;
            }
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            //HandleInput(gameTime);

            base.Update(gameTime);

            switch (GameState.CurrentState)
            {
                case GameStates.MENU:
                    mainMenu.Update(gameTime);
                    break;

                case GameStates.CREATE_GAME:
                    createGameMenu.Update(gameTime);
                    break;

                case GameStates.JOIN_GAME:
                    joinGameMenu.Update(gameTime);
                    break;

                case GameStates.PLAYER_JOIN_WAIT:
                    joinWaitScreen.Update(gameTime);
                    break;

                case GameStates.GAME_RUNNING:
                    gameManager.Update(gameTime);
                    break;

                case GameStates.MATCH_REPORT:
                    matchReportViewer.Update(gameTime);
                    break;

                default:
                    break;
            }
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);

            switch (GameState.CurrentState)
            {
                case GameStates.MENU:
                    mainMenu.Draw(gameTime);
                    break;

                case GameStates.CREATE_GAME:
                    createGameMenu.Draw(gameTime);
                    break;

                case GameStates.JOIN_GAME:
                    joinGameMenu.Draw(gameTime);
                    break;

                case GameStates.PLAYER_JOIN_WAIT:
                    joinWaitScreen.Draw(spriteBatch);
                    break;

                case GameStates.GAME_RUNNING:
                    gameManager.Draw(spriteBatch);
                    break;

                case GameStates.MATCH_REPORT:
                    matchReportViewer.Draw(spriteBatch);
                    break;

                default:
                    break;
            }

            base.Draw(gameTime);
            spriteBatch.End();
        }
    }
}
